package com.scs.application.modules.fsd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.PinYinUtils;
import com.scs.application.modules.fsd.entity.FsdChestGroup;
import com.scs.application.modules.fsd.mapper.ChestGroupMapper;
import com.scs.application.modules.fsd.service.ChestGroupService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 */
@Service
public class ChestGroupServiceImpl extends BaseServiceImpl<ChestGroupMapper, FsdChestGroup> implements ChestGroupService {
    @Override
    public Map<String, Object> getChestGroupList(String keyword, Integer currentPage, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", this.count(Wrappers.<FsdChestGroup>query().like("chest_group_name", keyword).or().like("chest_group_name_py", keyword).or().like("remark", keyword)));
        map.put("rows", this.list(
                Wrappers.<FsdChestGroup>query().like("chest_group_name", keyword).or().like("chest_group_name_py", keyword).or().like("remark", keyword).last("limit " + (currentPage - 1) * pageSize + "," + pageSize).orderByDesc("id")
        ));
        return map;
    }

    @Override
    public Boolean createChestGroup(String chestGroupName, String remark) {
        FsdChestGroup chestGroup = new FsdChestGroup();
        chestGroup.setChestGroupName(chestGroupName);
        chestGroup.setChestGroupNamePy(PinYinUtils.getAlpha(chestGroupName));
        chestGroup.setRemark(remark);
        save(chestGroup);
        return true;
    }

    @Override
    public Boolean editChestGroupById(String chestGroupName, String remark, String id) {
        FsdChestGroup chestGroup = this.getOne(Wrappers.<FsdChestGroup>query()
                .eq("id", id));
        if (chestGroup == null) throw new BusinessException("无效的柜组");
        chestGroup.setChestGroupName(chestGroupName);
        chestGroup.setChestGroupNamePy(PinYinUtils.getAlpha(chestGroupName));
        chestGroup.setRemark(remark);
        this.updateById(chestGroup);
        return true;
    }

    @Override
    public List<FsdChestGroup> getAll() {
        return this.list(Wrappers.<FsdChestGroup>query().eq("status",0));
    }

}
